package com.bootdo.quota.controller;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.quota.domain.QuotaHistoryDO;
import com.bootdo.quota.service.QuotaService;

/**
 * @Desc 额度流水
 * @date 2020/1/14
 *
 */
@Controller
@RequestMapping("/quota/quota")
public class QuotaController {

    private final String PREFIX = "quota";

    @Autowired
    private QuotaService quotaService;


    @GetMapping()
    @Log("获取平台流水")
    @RequiresPermissions("quota:quota:list")
    String quota(Model model) {
        model.addAttribute("id", getUserId() == 1L ? 2L : getUserId());
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<QuotaHistoryDO> userList = quotaService.list(query);
        int total = quotaService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }

    @GetMapping("/excelDownload")
    @Log("导出excel")
    @RequiresPermissions("quota:quota:export")
    @ResponseBody
    void excelDownload(@RequestParam Map<String, Object> params) throws IOException {
    	HSSFWorkbook workbook = null;
    	try{
	        workbook = quotaService.excelDownload(params);
	        String fileName = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + "_quota.xls";
	
	        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	        HttpServletResponse response = servletRequestAttributes.getResponse();
	        //八进制输出流
	        response.setContentType("application/octet-stream");
	        //这后面可以设置导出Excel的名称，此例中名为student.xls
	        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
	        //刷新缓冲
	        response.flushBuffer();
	        //workbook将Excel写入到response的输出流中，供页面下载
	        workbook.write(response.getOutputStream());
        }finally{
        	if(null!=workbook){
        		workbook.close();
        	}
        }
    }
}
